import { defineStore } from 'pinia'
import { topCategory } from './../../constant/top-category'
import { findAllCategory } from '../../service/api/category'
import { ICategory } from '../../service/api/type/category.type'

const categoryStore = defineStore({
  //id唯一且必填
  id: 'category',
  state: () => {
    return {
      topCategoryList: topCategory
    }
  },

  // 开启数据缓存
  persist: {
    enabled: true
  },
  actions: {
    async setTopCategoryList() {
      const response = await findAllCategory()
      this.topCategoryList = response.result
      this.topCategoryList.forEach((item) => (item.open = false))
    },
    show(item: ICategory) {
      const category = this.topCategoryList.find(
        (category) => category.id === item.id
      )
      category!.open = true
    },
    hide(item: ICategory) {
      const category = this.topCategoryList.find(
        (category) => category.id === item.id
      )
      category!.open = false
    }
  }
})

export default categoryStore
